package pers.cz.mxgraph;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import pers.cz.flowcell.JefFlowCell;
import pers.cz.flowcell.JefFlowRoot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: Reids
 * @description: MxGraphModel
 * @author: Cheng Zhi
 * @create: 2023-01-06 10:24
 **/
public class MxGraphModel implements JefFlowRoot {

    private List<MxCell> mxCells = new ArrayList<>();

    private List<MxObject> mxObjects = new ArrayList<>();

    private List<MxEdge> mxEdges = new ArrayList<>();

    private Map<Integer, MxCell> mxCellMap = new HashMap<Integer, MxCell>();

    private MxCell startCell;

    public MxGraphModel(Document document) {
        Element rootElement = document.getRootElement();
        List<Element> elements = rootElement.elements();
        readElement(elements);
    }

    /**
     * 递归解析xml
     * @param elements
     */
    public void readElement(List<Element> elements) {

        for (Element element : elements) {
            if (element.getName().equals("mxCell")) {

                // 判断是否为边
                if (isEdge(element)) {
                    MxEdge mxEdge = new MxEdge(element);
                    this.mxEdges.add(mxEdge);
                } else {
                    MxCell mxCell = new MxCell(element, this);
                    if (mxCell.isStart()) {
                        this.startCell = mxCell;
                    }
                    Integer id = mxCell.getId();
                    this.mxCellMap.put(id, mxCell);
                    this.mxCells.add(mxCell);
                }

            }

            if (element.getName().equals("Object")) {
                MxObject mxObject = new MxObject(element);
                this.mxObjects.add(mxObject);
            }

            if (element.elements().size() > 0) {
                readElement(element.elements());
            }
        }
    }

    public Boolean isEdge(Element element) {

        Attribute edge = element.attribute("edge");
        if (edge == null) {
            return false;
        }
        return true;
    }

    public List<MxCell> getMxCells() {
        return mxCells;
    }

    public List<MxObject> getMxObjects() {
        return mxObjects;
    }

    public List<MxEdge> getMxEdges() {
        return mxEdges;
    }

    /**
     * 获取Map描述的节点集合
     * @return
     */
    public Map<Integer, MxCell> getMxCellMap() {
        return mxCellMap;
    }

    /**
     * 获取节点ID对应的节点
     * @param id
     * @return
     */
    public MxCell getMxCell(Integer id) {

        return mxCellMap.get(id);
    }

    /**
     * 获取开始节点
     * @return
     */
    public JefFlowCell getStartCell() {

        if (startCell == null) {
            throw new RuntimeException("不合法的流程");
        }
        return startCell;
    }

    @Override
    public List<JefFlowCell> getCells() {
        return null;
    }

    @Override
    public void addCell(JefFlowCell jefFlowCell) {

    }

    @Override
    public JefFlowCell getCellById(Integer id) {
        return null;
    }
}
